package mybatisCodeGenerator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: mybatis代码生成器
 * @author: 1246263205@qq.com
 * @create: 2020-10-08 14:04
 */
public class MybatisCodeGenerator {

    //基本设置
    private static String TABLE_NAME = "dy_activities_user_record";     //table名字 , 多个表用,隔开
    private static String[] TABLE_PREFIXES = {"t_", "m_", "s_", "tx_","aa_","dy_"};            //table前缀
    private static String AUTHOR_NAME = "yi";                //作者
    private static String PARENT_PACKAGE_NAME = "com.cloud.common";         //父包名
    private static String PATH = System.getProperty("user.dir");
    private static String URL = "jdbc:mysql://39.103.235.23:3306/fuya_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
    private static String DB_USERNAME = "scxb";
    private static String DB_PASSWORD = "i&D,Ixa#YJs[=n";
    private static String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";
    //生成相关设置
    private static Boolean IS_FILE_OVERRIDE = Boolean.TRUE;  // 是否覆盖文件
    private static Boolean IS_ENTITY_EXTENDS_MODEL = Boolean.FALSE;  // 是否继承mybatis-plus的model类
    private static Boolean IS_XML_ENABLE_CACHE = Boolean.FALSE; // mapper.xml 二级缓存
    private static Boolean IS_XML_KEEP_BASE_RESULT_MAP = Boolean.TRUE;  // mapper.xml 生成 ResultMap
    private static Boolean IS_XML_KEEP_BASE_COLUMN_LIST = Boolean.TRUE;  // mapper.xml 生成 columnList
    private static Boolean IS_ENTITY_COLUMN_CONSTANT = Boolean.TRUE;  // mapper.xml 生成 columnList
    private static Boolean IS_OUT_CONTROLLER = Boolean.FALSE;  // 是否生成controller


    //运行main方法即可执行,或者运行类即可   !!!需要当心覆盖文件为true的情况
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();


        mpg.setGlobalConfig(new GlobalConfig().         // 全局配置
                setOutputDir(PATH + "/src/main/java")  //类输出位置
                .setAuthor(AUTHOR_NAME) //作者
                .setOpen(false)

                .setSwagger2(true) //实体属性 Swagger2 注解);
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setFileOverride(IS_FILE_OVERRIDE)
                .setActiveRecord(IS_ENTITY_EXTENDS_MODEL)
                .setEnableCache(IS_XML_ENABLE_CACHE)
                .setBaseResultMap(IS_XML_KEEP_BASE_RESULT_MAP)
                .setBaseColumnList(IS_XML_KEEP_BASE_COLUMN_LIST)
        )
                .setDataSource(
                        // 数据源配置
                        new DataSourceConfig()
                                .setDriverName(DRIVER_CLASS_NAME)
                                .setUsername(DB_USERNAME)
                                .setPassword(DB_PASSWORD)
                                .setUrl(URL)
                ).setStrategy(
                new StrategyConfig()
                        .setTablePrefix(TABLE_PREFIXES)
                        .setNaming(NamingStrategy.underline_to_camel)
                        .setColumnNaming(NamingStrategy.underline_to_camel)
                        .setEntityLombokModel(true)
                        .setRestControllerStyle(true)
//                        .setSuperControllerClass(GeneratorConfig.PACKAGE_NAME + "." + GeneratorConfig.MODULE_NAME + ".common.controller.BaseController")
                        .setInclude(TABLE_NAME.split(","))
                        .setEntityColumnConstant(IS_ENTITY_COLUMN_CONSTANT)
                        .setControllerMappingHyphenStyle(true)
        ).setPackageInfo(
                // 包配置
                new PackageConfig()
                        //.setModuleName("User")
                        .setParent(PARENT_PACKAGE_NAME)// 自定义包路径
                        .setController("controller")// 这里是控制器包名，默认 web
                        .setEntity("entity")
                        .setMapper("mapper")
                        .setService("service")
                        .setServiceImpl("service.impl")
                //.setXml("mapper")
        ).setCfg(
                // 注入自定义配置，可以在 VM 中使用 cfg.abc 设置的值
                new InjectionConfig() {
                    @Override
                    public void initMap() {
                        Map<String, Object> map = new HashMap<>();
                        map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                        this.setMap(map);
                    }
                }.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig("/templates/mapper.xml.vm") {
                    // 自定义输出文件目录
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return PATH + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
                    }
                }))
        );
        TemplateConfig tc = new TemplateConfig().setXml(null);
        if (!IS_OUT_CONTROLLER) {
            tc.setController("");
        }
        mpg.setTemplate(
                tc
        );
        mpg.execute();
    }

}